package com.flycms.module.offline.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flycms.module.offline.dao.OfflineActivityDao;
import com.flycms.module.offline.model.OfflineActivity;
import com.flycms.module.offline.model.OfflineActivitySign;

/**
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年3月2日
 * @Date 2020年3月2日
 */

@Service
public class OfflineActivityService {

    @Autowired
    OfflineActivityDao offlineDao;
    
    @Transactional
    public int addOfflineActivity(OfflineActivity offline) throws Exception {
        return offlineDao.addOfflineActivity(offline);
    }
    
    @Transactional
    public int editOfflineActivity(Long id, String name, String position, Integer status, Date updateTime,
            Date startTime, Date endTime, String content) throws Exception {
        OfflineActivity offline = new OfflineActivity();
        offline.setId(id);
        offline.setName(name);
        offline.setStatus(status);
        offline.setPosition(position);
        offline.setUpdateTime(updateTime);
        offline.setStartTime(startTime);
        offline.setEndTime(endTime);
        offline.setContent(content);
        return offlineDao.editOfflineActivity(offline);
    }
    
    public List<OfflineActivity> getOfflineActivityList(String name, String position, Integer status,
            String orderby, String order, int pageIndex, int pageSize) throws Exception {
        if (StringUtils.isNotEmpty(orderby)) {
            orderby = "create_time";
        } else {
            orderby = "create_time";
        }
        return offlineDao.getOfflineActivityList(name, position, status, orderby, order, pageIndex, pageSize);
    }
    
    public int getOfflineActivityCount(String name, String position, Integer status) throws Exception {
        return offlineDao.getOfflineActivityCount(name, position, status);
    }
    
    public List<OfflineActivitySign> getSelfOfflineActivityList(Long userId, String name, String position,
            Integer status, int pageIndex, int pageSize) throws Exception {
        return offlineDao.getSelfOfflineActivityList(userId, name, position, status, pageIndex, pageSize);
    }
    
    public int getSelfOfflineActivityCount(Long userId, String name, String position, Integer status) throws Exception {
        return offlineDao.getSelfOfflineActivityCount(userId, name, position, status);
    }
    
    public OfflineActivity queryOfflineActivityById(Integer id) throws Exception {
        return offlineDao.queryOfflineActivityById(id);
    }
    
    public List<OfflineActivity> getOfflineActivityListCalendar() throws Exception {
        List<OfflineActivity> list = offlineDao.getOfflineActivityNotEnd(5);
        if (list.size() == 5) {
            return list;
        } else {
            list.addAll(offlineDao.getOfflineActivityEnd(5 - list.size()));
            return list;
        }
    }
    
    @Transactional
    public int offlineActivityStatusUpdate() {
        Date now = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String date = format.format(now);
//        offlineDao.editOfflineActivityIng(OfflineActivityStatus.ACTIVITY_ING.value(), date);
//        offlineDao.editOfflineActivityEnd(OfflineActivityStatus.ACTIVITY_END.value(), date);
        offlineDao.updateOfflineActivity(date);
        return 1;
    }
    
    @Transactional
    public int removeOfflineActivity(Long id) throws Exception {
        return offlineDao.removeOfflineActivity(id);
    }
    
}
